package com.app;

import com.bean.Bean1;
import com.bean.Bean2;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;
import scala.Tuple2;

/**
 * @Description: TODO QQ1667847363
 * @author: xiao kun tai
 * @date:2022/1/5 21:28
 */
public class FlinkDataStreamJoinApp {
    public static void main(String[] args) throws Exception {
        //TODO:1.获取执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //TODO:2.读取两个端口数据创建并提取时间戳生产WaterMark
        SingleOutputStreamOperator<Bean1> stream1 = env.socketTextStream("192.168.88.109", 8888)
                .map(line -> {
                    String[] arr = line.split(",");
                    return new Bean1(arr[0], arr[1], Long.parseLong(arr[2]));
                }).assignTimestampsAndWatermarks(WatermarkStrategy.<Bean1>forMonotonousTimestamps()
                        .withTimestampAssigner(new SerializableTimestampAssigner<Bean1>() {
                            @Override
                            public long extractTimestamp(Bean1 bean1, long l) {
                                return bean1.getTs() * 1000L;
                            }
                        }));


        SingleOutputStreamOperator<Bean2> stream2 = env.socketTextStream("192.168.88.109", 9999)
                .map(line -> {
                    String[] arr = line.split(",");
                    return new Bean2(arr[0], arr[1], Long.parseLong(arr[2]));
                }).assignTimestampsAndWatermarks(WatermarkStrategy.<Bean2>forMonotonousTimestamps()
                        .withTimestampAssigner(new SerializableTimestampAssigner<Bean2>() {
                            @Override
                            public long extractTimestamp(Bean2 bean2, long l) {
                                return bean2.getTs() * 1000L;
                            }
                        }));


        //TODO:3.双流JOSN
        SingleOutputStreamOperator<Tuple2<Bean1, Bean2>> joinDS = stream1.keyBy(Bean1::getId)
                .intervalJoin(stream2.keyBy(Bean2::getId))
                .between(Time.seconds(-5), Time.seconds(5))
//                .lowerBoundExclusive()   //左边不包括
//                .upperBoundExclusive()   //右边不包括
                .process(new ProcessJoinFunction<Bean1, Bean2, Tuple2<Bean1, Bean2>>() {
                    @Override
                    public void processElement(Bean1 bean1, Bean2 bean2, Context context, Collector<Tuple2<Bean1, Bean2>> collector) throws Exception {
                        collector.collect(new Tuple2<>(bean1, bean2));
                    }
                });


        //TODO:4.打印
        joinDS.print("");

        //TODO:5.启动
        env.execute();
    }
}
